﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HoldemView
{
    public partial class CardDropDown : Control
    {
        //private string sCards = "XX XX XX";
        private string sRanks = "23456789TJQKA";
        private string sSuits = "cdhs";
        private RichTextBox host;
        private SizeF sz;

        private struct Three
        {
            public int X;
            public int Y;
            public int B;

            public Three(int x, int y, int b){
                X = x; Y = y; B = b;
            }
        }

        private Three? cursor;

        //private Font font;

        public CardDropDown(RichTextBox h)
        {
            InitializeComponent();
            host=h;

            this.Font = h.Font;
            this.Margin = new Padding(0, 0, 0, 0);

            Graphics g = h.CreateGraphics();

            // Get the Size needed to accommodate the formatted Text.
            Size preferredSize = g.MeasureString(
               h.Text, h.Font).ToSize();

            // Pad the text and resize the control.
            this.Width = preferredSize.Width;
            this.Height = preferredSize.Height*NumRanks;

            this.sz = g.MeasureString("7", Font, 100, StringFormat.GenericTypographic).ToSize();

            // Clean up the Graphics object.
            g.Dispose();


            this.DoubleBuffered = true;

            this.MouseDown += new MouseEventHandler(CardDropDown_MouseDown);
            this.MouseMove += new MouseEventHandler(CardDropDown_MouseMove);
        }

        private int NumRanks { get { return sRanks.Length; } }
        private int NumSuits { get { return sSuits.Length; } }

        void CardDropDown_MouseMove(object sender, MouseEventArgs e)
        {
            int o = 0, y = 0, b=0;
            if (ClientToCoords(out o, out y, out b, e.Location))
            {
                if (null == cursor || cursor.Value.X != o || cursor.Value.Y != y || cursor.Value.B!=b)
                {
                    cursor = new Three(o, y, b);
                    Refresh();
                }
            }
            else
            {
                cursor = null;
                Refresh();
            }
        }

        private bool ClientToCoords(out int o, out int y, out int b, Point e)
        {
            b = (int)(e.X / sz.Width / 3);
            o = (int)((e.X - 3 * b * sz.Width) / sz.Width);
            y = (int)(e.Y / sz.Height);
            return o == 0 && y < NumRanks || o == 1 && y < NumSuits;
        }

        void CardDropDown_MouseDown(object sender, MouseEventArgs e)
        {
            int o = 0, y = 0, b=0;
            if (!ClientToCoords(out o, out y, out b,e.Location))
                return;
            if(o==0)
            {
                string s = host.Text;
                s = s.Substring(0,3*b)+sRanks[y]+s.Substring(3*b+1);
                host.Text=s;
            }
            if(o==1)
            {
                string s = host.Text;
                s = s.Substring(0, 3 * b+1) + sSuits[y] + s.Substring(3 * b + 2);
                host.Text = s;
            }
            this.Refresh();

        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Draw(pe.Graphics);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            return;
        }

        public static ToolStripDropDown DropDown(RichTextBox h)
        {
            Control cdd = new CardDropDown(h);
            Point loc = h.PointToScreen(h.Bounds.Location);
            cdd.Location = loc;
            cdd.Visible = true;
            
            ToolStripControlHost host = new ToolStripControlHost(cdd);

            // create drop down and add it

            ToolStripDropDown dropDown = new ToolStripDropDown();
            dropDown.Items.Add(host);
           // dropDown.Width = 200;
            //dropDown.Height = 300;
            dropDown.Show(h, 0,h.Height);
            return dropDown;
            
        }

        protected string sCards { get { return host.Text; } }
        protected int NumCards { get { return (sCards.Length+1) / 3; } }

        protected virtual void Draw(Graphics g)
        {
            float W = 12;
            float H = 12;
            Brush bk = new SolidBrush(Color.White);
            g.FillRectangle(bk, this.ClientRectangle);

            Pen pen = new Pen(Color.DarkBlue);
            if(null!=cursor)
                g.DrawRectangle(pen, (cursor.Value.B*3+cursor.Value.X) * sz.Width, cursor.Value.Y * sz.Height, sz.Width, sz.Height);


            for(int b=0;b<NumCards;b++)
            {
                string mr = sCards.Substring(3*b,1);
                string ms = sCards.Substring(3*b+1,1);

                for(int r=0;r<sRanks.Length;r++)
                {
                    string sr = sRanks.Substring(r, 1);
                    Color clr = sr.Equals(mr,StringComparison.OrdinalIgnoreCase)?Color.Red:Color.Black;
                    Brush br = new SolidBrush(clr);
                    g.DrawString(sr, Font, br, b * sz.Width * 3, r * sz.Height, StringFormat.GenericTypographic);
                }
                for(int s=0;s<sSuits.Length;s++)
                {
                    string ss = sSuits.Substring(s, 1);
                    Color clr = ss.Equals(ms,StringComparison.OrdinalIgnoreCase)?Color.Red:Color.Black;
                    Brush br = new SolidBrush(clr);

                    g.DrawString(ss, Font, br, (b * 3 + 1) * sz.Width, s * sz.Height, StringFormat.GenericTypographic);
                }


            }
        }
    }
}
